package de.tmahr.android.animation;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;

public class KreisAnimation implements RenderView.Animation
{
	private ArrayList<Kreis> kreise = null;
	private Paint paint;
	private int backgroundColor;
	private int textColor;
	private int stepCounter;

	public KreisAnimation()
	{
		paint = new Paint();
		paint.setAntiAlias(true);
		backgroundColor = Color.rgb(0,0,0);
		setTextColor();
	}

	@Override
	public void render(Canvas canvas)
	{
		if (kreise == null)
		{
			kreise = new ArrayList<Kreis>();
			for (int i = 0; i < 100; i++)
			{
				kreise.add(new Kreis(canvas.getWidth(), canvas.getHeight()));
			}
		}

		paint.setStyle(Style.FILL);

		canvas.drawColor(backgroundColor);
		for (Kreis k : kreise)
		{
			paint.setARGB(k.alpha(), k.rot(), k.gruen(), k.blau());
			canvas.drawCircle(k.x(), k.y(), k.radius(), paint);
			k.bewege();
		}
		
		overlayStepCounter(canvas);
	
		stepCounter++;
	}
	
	private void overlayStepCounter(Canvas canvas)
	{
		paint.setStyle(Paint.Style.FILL);
		paint.setStrokeWidth(1);
		paint.setColor(textColor);
		paint.setTextSize(10);
		String s = String.valueOf(stepCounter);
		Rect rect = new Rect();
		paint.getTextBounds(s, 0, s.length(), rect);
		canvas.drawText(s, 0, -rect.top, paint);
/*	
		// draw some text using FILL style
		paint.setStyle(Paint.Style.FILL);
		//turn antialiasing on
		paint.setAntiAlias(true);
		paint.setTextSize(10);
		canvas.drawText("Style.FILL", 15, 110, paint);
	
		// draw some rotated text
		// get text width and height
		// set desired drawing location
		int x = 15;
		int y = 85;
		paint.setColor(Color.GRAY);
		paint.setTextSize(25);
		String str2rotate = "Rotated!";
	
		// draw bounding rect before rotating text
		rect = new Rect();
		paint.getTextBounds(str2rotate, 0, str2rotate.length(), rect);
		canvas.translate(x, y);
		paint.setStyle(Paint.Style.FILL);
		// draw unrotated text
		canvas.drawText("!Rotated", 0, 0, paint);
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawRect(rect, paint);
		// undo the translate
		canvas.translate(-x, -y);
	
		// rotate the canvas on center of the text to draw
		canvas.rotate(-45, x + rect.exactCenterX(),
	                                       y + rect.exactCenterY());
		// draw the rotated text
		paint.setStyle(Paint.Style.FILL);
		canvas.drawText(str2rotate, x, y, paint);
	
		//undo the rotate
		canvas.restore();
		canvas.drawText("After canvas.restore()", 50, 25, paint);	
		
		canvas.drawLine(0, canvas.getHeight()/2, canvas.getWidth(), canvas.getHeight()/2, paint);
		canvas.drawLine(canvas.getWidth()/2, 0, canvas.getWidth()/2, canvas.getHeight(), paint);
*/		
	}

	@Override
	public void setBackgroundColor(int c)
	{
		backgroundColor = c;
		setTextColor();
	}
	
	private void setTextColor()
	{
		textColor = Color.rgb(255-Color.red(backgroundColor), 255-Color.green(backgroundColor),255-Color.blue(backgroundColor));
	}
}